package com.example.mystore.service;

import com.example.mystore.model.Category;
import com.example.mystore.model.Product;
import com.example.mystore.repository.CategoryRepository;
import com.example.mystore.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryService {

    @Autowired
    private CategoryRepository categoryRepository;

    @Autowired
    private ProductRepository productRepository;

    // 获取所有分类
    public List<Category> getAllCategories() {
        return categoryRepository.findAll();
    }

    // 获取分类下的商品
    public List<Product> getProductsByCategory(Long categoryId, double minPrice, double maxPrice, String sort) {
        return productRepository.findByCategoryId(categoryId, Sort.by(sort));
    }
}
